<template>
  <div class="singleImageUpload2 upload-container">
    <el-upload
      ref="upload"
      :data="dataObj"
      :multiple="false"
      :show-file-list="false"
      :on-success="handleImageSuccess"
      class="image-uploader"
      drag
      :action="baseUrl"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">
        拖拽或<em>点击上传</em>
      </div>
    </el-upload>
    <div v-show="imageUrl" class="image-preview">
      <div class="image-preview-wrapper">
        <img :src="imageUrl" />
        <div class="image-preview-action">
          <span class="change-img" @click="changeImage">更换图片</span>
          <span class="rm-img">
            <i class="iconfont iconclose" @click="rmImage" />
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SingleImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    limit: {
      type: Number,
      default: 1
    },
    url: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tempUrl: '',
      imageUrl: this.filterImageUrl(this.value),
      baseUrl: process.env.VUE_APP_BASE_API + '/api/upload',
      dataObj: { token: '', key: '' }
    }
  },
  watch: {
    value: {
      handler(newValue, oldValue) {
        if (newValue !== oldValue && newValue !== null) {
          if (newValue.includes("http") || newValue === '') {
            this.imageUrl = this.value
          }
        }
      }
    }
  },
  methods: {
    rmImage() {
      this.imageUrl = ''
      this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    changeImage() {
      document.querySelector('.el-upload__text').click()
    },
    handleImageSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.emitInput(res.data.path)
    },
    filterImageUrl(val) {
      // !this.value.includes("http") && this.value.includes("data") ? `${this.url}/` + this.value : this.value
      if (val) {
        return !val.includes("http") && val.includes("data") ? `${this.url}/` + val : val
      }

      return val
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-container {
  width: 100%;
  height: 100%;
  position: relative;
  .image-uploader {
    height: 100%;
  }
  .image-preview {
    width: 100%;
    height: 200px;
    position: absolute;
    border-radius: 6px;
    border: 1px dashed #d9d9d9;
    top: 0;
    left: 0;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 6px;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      // background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      border-radius: 6px;
      .change-img {
        transition: all .2s;
        background-color: rgba(24, 144, 255, .8);
        padding: 8px 25px;
        border-radius: 25px;
        color: #fff;
        &:hover {
          background-color: rgba(24, 144, 255, 1);
        }
      }
      .rm-img {
        position: absolute;
        right: 20px;
        top: 20px;
        background-color: rgba(62, 72, 80, .8);
        display: inline-block;
        width: 25px;
        height: 25px;
        line-height: 25px;
        border-radius: 50%;
        &:hover {
          background-color: rgba(62, 72, 80, 1);
        }
      }
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
}
</style>
